import math


class Solution(object):
    def nthUglyNumber(self, n, a, b, c):
        ab = a * b // math.gcd(a, b)
        ac = a * c // math.gcd(a, c)
        bc = b * c // math.gcd(b, c)
        abc = ab * c // math.gcd(ab, c)

        def search(val):
            n1 = val // a + val // b + val // c
            n2 = val // ab + val // ac + val // bc
            n3 = val // abc
            return n1 - n2 + n3

        left = min(a, b, c)
        right = 2 * 10 ** 9
        while left < right:
            mid = (left + right) // 2
            num = search(mid)
            if num >= n:
                right = mid
            else:
                left = mid + 1
        return left